/*
package com.iris.live.services.services.impl;

import com.google.common.collect.Maps;
import com.iris.live.services.common.PwdUtils;
import com.iris.live.services.common.StringUtils;
import com.iris.live.services.data.models.*;
import com.iris.live.services.data.repositories.*;
import com.iris.live.services.models.PageData;
import com.iris.live.services.models.ResultData;
import com.iris.live.services.services.GroupUserService;
import com.iris.live.services.vo.UserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.*;

*/
/**
 * @author: wangzhenyan
 * date: 2017/9/26 9:52
 *//*

@Service
@Transactional
public class GroupUserServiceImpl implements GroupUserService {

    private static final Logger logger = LoggerFactory.getLogger(GroupUserServiceImpl.class);

    @Resource
    private GroupUserRepository groupUserRepository;

    @Resource
    private Lsh经销商表Repository lsh经销商表Repository;

    @Resource
    private 用户Repository userRepository;

    @Resource
    private AccountJpaRepository accountJpaRepository;

    @Resource
    private PushApprovalWorkFlowRepository pushApprovalWorkFlowRepository;

    @Override
    @Transactional(readOnly = true)
    public ResultData getGroupRoles() {
        ResultData result = new ResultData("true", null, null);
        // 存放最终集合
        List<Map<String, String>> mapList = new ArrayList<>();
        // 得到所有的角色
        List<Map<String, String>> roles = groupUserRepository.getGroupRoles();
        if (roles != null && !roles.isEmpty()) {
            for (Map<String, String> role : roles) {
                Map<String, String> temp = new HashMap<>();
                if ("集团".equals(role.get("roleName"))) {
                    temp.put("管理范围", "集团");
                    temp.put("大区", "");
                    temp.put("PMA", "");
                } else if ("TJLS".equals(role.get("roleName"))) {
                    temp.put("管理范围", "TJLS");
                    temp.put("大区", "");
                    temp.put("PMA", "");
                } else if ("大区".equals(role.get("roleName"))) {
                    List<Lsh经销商表Model> bigAreas = lsh经销商表Repository.getBigArea(); // 所有大区
                    if (bigAreas != null && !bigAreas.isEmpty()) {
                        for (Lsh经销商表Model bigArea : bigAreas) {
                            Map<String, String> temp2 = new HashMap<>();
                            temp2.put("管理范围", "大区");
                            temp2.put("大区", bigArea.get大区());
                            temp2.put("PMA", "");
                            mapList.add(temp2);
                        }
                    }

                } else if ("PMA".equals(role.get("roleName"))) {
                    List<Lsh经销商表Model> ctas = lsh经销商表Repository.getListGroupByCta(); // 所有小区
                    if (ctas!=null && !ctas.isEmpty()) {
                        for (Lsh经销商表Model cta : ctas) {
                            Map<String, String> temp2 = new HashMap<>();
                            temp2.put("管理范围", "PMA");
                            temp2.put("大区", cta.get大区());
                            temp2.put("PMA", cta.getCta());
                            mapList.add(temp2);
                        }
                    }
                }
                if (temp.size() > 0) {
                    mapList.add(temp);
                }
            }
        }
        result.setPageData(new PageData(mapList, mapList.size()));
        return result;
    }

    @Override
    @Transactional(readOnly = true)
    public ResultData getGroupUsers(GroupUserRequest userRequest) {
        ResultData result = new ResultData("true",null,null);
        PageData pageData = groupUserRepository.getGroupUsers(userRequest);
        result.setPageData(pageData);
        return result;
    }

    @Override
    public Map updateGroupUserStatus(用户Model userVo) {
        */
/*String disabledStatus = userVo.getDisabledStatus();
        Map result = new HashMap<>();
        result.put("result","true");
        result.put("message","操作成功");

        用户Model user = userRepository.findUserInfo(userVo.get用户名(),userVo.get许可());
        if (user == null) {
            result.put("result","false");
            result.put("message","操作失败，用户不存在");
            return result;
        }

        if ("停用".equals(disabledStatus)) {
            // 检查该用户是否有未完成的审批
            List<PushApprovalWorkFlowModel> approvalModels = pushApprovalWorkFlowRepository.getApprovalRecordByUserNameAndStatus(userVo.get用户名(),"0");
            if (approvalModels != null && !approvalModels.isEmpty()) {
                result.put("result","false");
                result.put("message","，该用户有审批流程未完成请与其联系再停用。");
                return result;
            }
        }

        user.setDisabledStatus("停用".equals(disabledStatus) ? "1" : "0");
        userRepository.save(user);
        return result;*//*


        return null;
    }

    @Override
    @Transactional(readOnly = true)
    public ResultData getRoleDepartmentRelationMap() {
        List list = groupUserRepository.getRoleDepartmentRelationMap();
        return new ResultData("true",null,new PageData(list,list.size()));
    }

    @Override
    @Transactional(readOnly = true)
    public ResultData getGroupItems(GroupItemsRequest params) {
        ResultData result = new ResultData("true",null,null);
        List<Map<String,Object>> items = new ArrayList<>(); // 最终要返回的权限
        List<Map<String,Object>> allItems = groupUserRepository.getAllGroupItems(); // 所有的权限

        // 选中部门时，返回选中的部门的权限
        if (!StringUtils.isNullOrBlank(params.departmentName)) {
            if (!allItems.isEmpty()) {
                List<Map<String,Object>> departItems = groupUserRepository.getDepartItems(params); // 选中的部门的默认权限
                for (Map<String,Object> map : allItems) {
                    // 勾选部门的权限
                    if (!departItems.isEmpty() && departItems.contains(map)) {
                        map.put("check","1");
                    } else {
                        map.put("check","0");
                    }
                }
            }
            items = allItems;
        }
        // 编辑时，返回用户设置的权限
        else if (!StringUtils.isNullOrBlank(params.userName)) {
            if (!allItems.isEmpty()) {
                List<Map<String,Object>> userItems = groupUserRepository.getUserGroupItems(params);  // 用户设置的权限
                for (Map<String,Object> map : allItems) {
                    // 勾选用户的的权限
                    if (!userItems.isEmpty() && userItems.contains(map)) {
                        map.put("check","1");
                    } else {
                        map.put("check","0");
                    }
                }


            }
            items = allItems;
        }
        // 新增时，返回所有的权限
        else {
            if (!allItems.isEmpty()) {
                for (Map<String,Object> map : allItems) {
                    map.put("check","0");
                }
            }
            items = allItems;
        }
        PageData pageData= new PageData(items,items.size());
        result.setPageData(pageData);
        return result;
    }

    @Override
    public Map saveGroupUser(UserVo userVo) {
        */
/*Map result = new HashMap();
        result.put("result","true");
        result.put("message","保存成功");

        用户Model userTemp;
        用户Model user = userRepository.findUserInfo(userVo.get用户名(),userVo.get许可());
        if (user == null) {
            userTemp = new 用户Model();
        } else {
            userTemp = user;

        }
        userTemp.set人员姓名(userVo.get人员姓名());
        userTemp.set人员电话(userVo.get人员电话());
//        userTemp.setGroupPositionCN(userVo.getGroupPositionCN());
//        userTemp.setGroupPositionEN(userVo.getGroupPositionEN());
        userTemp.setKpi使用状态(userVo.getKpi使用状态());
        List<Map<String,Object>> departments = groupUserRepository.getGroupDepartment(userVo.get所属部门());
        userTemp.set所属部门(departments.get(0).get("id").toString());
        if ("集团".equals(userVo.getManageScope()) || "TJLS".equals(userVo.getManageScope())) {
            userTemp.set许可("LSH");
        } else if("大区".equals(userVo.getManageScope())) {
            userTemp.set许可(userVo.getBigArea());
        } else if ("PMA".equals(userVo.getManageScope())) {
            userTemp.set许可(userVo.getPma());
        }
        userTemp.set角色(userVo.getManageScope());  // 管理范围设置为角色

        // 编辑
        if (user != null) {
            userTemp.set更新日期(new Timestamp(new Date().getTime()));
            userRepository.save(userTemp);

            // 保存权限之前，要删除该用户之前设置的权限
            groupUserRepository.deleteGroupUserItems(userVo.get许可(),userVo.get用户名());

        } else {  // 新增
            boolean isNewAccount = false;  // 是否是新账户 （一个账户可能多个角色）
            String pwd = "";

//            userTemp.setUserType("1");  // 用户类型（集团）
//            userTemp.setDisabledStatus("0");  // 启用
            userTemp.set删除状态("");
            userTemp.set更新日期(new Timestamp(new Date().getTime()));
            userTemp.set设置日期(new java.sql.Date(new Date().getTime()));

            //检查账号是存在
            AccountModel accountModel = accountJpaRepository.getAccountInfo(userVo.get用户名());
            if(accountModel == null){  //不存在，新建账号
                isNewAccount = true;
                pwd = PwdUtils.generatePwd();

                if(logger.isDebugEnabled()){
                    logger.debug("No account info found for username={}", userVo.get用户名());
                }
                accountModel = new AccountModel();
                accountModel.setAccountName(userVo.get用户名());
                accountModel.setAccountPwd(pwd);  //  生成随机密码
                accountJpaRepository.save(accountModel);
            }
            // 检查该登陆用户名在group端是否已经存在账号
            else {

                List<用户Model> groupUsers =  userRepository.findUserByUserNameForGroup(userVo.get用户名());
                if (groupUsers != null && !groupUsers.isEmpty()) {
                    result.put("result","false");
                    result.put("message","group中已存在该用户");
                    return result;
                }
            }
            String userName = userVo.get用户名();
            List<用户Model> models = userRepository.findAllUserInfoes(accountModel.getAccountId());
            int index = (models == null ? 1 : models.size() + 1);
            if(userName.contains("@")){
                userName = userName.substring(0, userName.indexOf("@")) + (index < 10 ? "0" + index : index) + userName.substring(userName.indexOf("@"));
            }else{
                userName = userName + (index < 10 ? "0" + index : index);
            }

            //创建用户信息
            userTemp.set密码(accountModel.getAccountPwd());
//            userTemp.setAccountId(accountModel.getAccountId());
            userTemp.set用户名(userName);  //设置用户名
            userRepository.save(userTemp);

            if (isNewAccount) { // 是新账户
                result.put("message","新增用户成功，密码是：" + pwd);
            } else {
                result.put("message","新增用户成功");
            }
        }

        // 保存新的权限
        if (!StringUtils.isNullOrBlank(userVo.getItemsIds())) {
            String[] itemIds = userVo.getItemsIds().split(",");
            if (itemIds.length > 0) {
                for (String str : itemIds) {
                    TGroupUserItemModel userModel = new TGroupUserItemModel();
                    userModel.setPermission(userTemp.get许可());
                    userModel.setUserName(userVo.get用户名());
                    userModel.setItemId(Integer.parseInt(str));
                    groupUserRepository.saveGroupUserItems(userModel);
                }
            }
        }*//*

        return null;
    }

    @Override
    public Map resetGroupUserPwd(GroupUserRequest params) {
        */
/*Map result = Maps.newHashMap();
        AccountModel accountModel = accountJpaRepository.getAccountByUserNameAndPermit(params.username,params.permit);
        if (accountModel == null) {
            result.put("result","false");
            result.put("message","重置失败，用户不存在");
            return result;
        }

        String pwdStr = PwdUtils.generatePwd();  // 新密码
        accountModel.setAccountPwd(pwdStr);
        accountJpaRepository.save(accountModel);

        // 更新该账号下所有用用的密码
        groupUserRepository.updateUserPwd(accountModel.getAccountId(),pwdStr);

        result.put("result","true");
        result.put("message","密码已重置，新密码为：" + pwdStr);
        return result;*//*


        return null;
    }
}
*/
